<?php
/*
----------------------------------------
-----    Page logic description    -----
----------------------------------------
REFERENCES FROM:
    * cp__mon.php (as include)
INPUT:
    m       => monitor object
OUTPUT: 
	Page
----------------------------------------
*/
    
if (!isset($account)) {
    die("Access denied: account is not loaded."); // TODO: make as a function
}

if (!isset($m)) {
    die("Access denied: monitor is not loaded."); // TODO: make as a function
}


$template_cp_row = '
<table class=dash [%TR_EXT%]>
  <tbody>
  <tr>
    <td class=dash-col1><div class=dash-div-col1>
        <table class=monitor-name>
        <tr>
        	<td class=checkbox>[%CHECKBOX%]</td>
        	<td class=icon>[%ICON%]</td>
        	<td class=name>[%MONITOR_NAME%]</td>
        	<td class=arrow><img src="../images/arr_closed.gif" id="cpMonArr_[%MID%]" width="6" height="5" alt="" border="0"></td>
        </tr>
        </table>
    </div></td>
    <td class=dash-col2><div class=dash-div-col2>[%GROUP_NAME%]</div></td>
    <td class=dash-col3><div class=dash-div-col3>[%STATUS%]</div></td>
    <td class=dash-col4><div class=dash-div-col4>[%MONITORED%]</div></td>
    <td class=dash-col5><div class=dash-div-col5>[%RESULT%]</div></td>
  </tr>
  </tbody>
</table>
';


GetResStorageList($m->id, 2, $rsl);

$url = @$m->data["url"];

// *** Title
$title = htmlspecialchars($m->data["title"]);
if ($title == "") $title = "Untitled";


$cl = '#F2F7FA';

// *** Checkbox
$str_checkbox =
	'<input type="Checkbox" id="cbMon_'.$m->id.'"'.
    " onClick=\"dbrd_row_action(".$m->id.", 'over', '$cl');\"".
    " onMouseOver=\"dbrd_row_action(".$m->id.", 'cb_over', '$cl');\"".
    " onMouseOut=\"dbrd_row_action(".$m->id.", 'cb_out', '$cl');\"".
    ">";


// [Icon + link] Name
$url2 = normalize_url($url);

if (@$m->data["favicon_content_type"]) {
    // Fav icon is saved in DB
    $icon = $r.'members/monitor_icon.php?mid='.$m->id;
    //$icon = $r.'images/favicon.ico';
}
else {
    // Set default icon
	switch (@$m->data["content_type"] & CONTENT_TYPE_MASK)
	{
		case CONTENT_TYPE_XML:
			$icon = "feed.gif";
			break;
		case CONTENT_TYPE_HTML:
		default:
			$icon = "glob.gif";
			break;
	}
    $icon = $r.'images/'.$icon;
}

$str_icon =
    '<a href="'.$url2.'" target=_blank>'.
    '<img src="'.$icon.'" width="16" height="16" alt="Click to visit '.$url2.'" border="0">'.
    '</a>';

// *** Status
if ($m->data["status"] & MONITOR_STATUS_IS_ERROR_MASK) {
    $status_text = 'error';
    $status_icon = 'error';
}     
else if ($m->data["status"] & MONITOR_STATUS_IS_ACCOUNT_LIMIT_MASK) {
    $status_text = 'free limit exceeded';
    $status_icon = 'error';
}
else if ($m->data["status"] & MONITOR_STATUS_IS_ACTIVE_MASK) {
    $status_text = 'active';
    $status_icon = 'active';
}
else {
    $status_text = 'paused';
    $status_icon = 'paused';
}

// *** Last Monitored
$str_monitored = get_last_monitored_periodicity($m->data["frequency"]) . ", ";
if (isset($rsl[0])) {
    //$dt = $account->TimeToUserTZ($rsl[0]->data["download_dt"]);
    //$str_monitored = time_to_str($dt, $g_delta_format_TM);
    $str_monitored .= get_last_monitored_time_str($m->data["rs_download_dt"]);
}
else {
    $str_monitored .= "not yet";
}

// *** Result
if (isset($rsl[0]))
    $str_result =
    '<a href="history_nav.php?monitor_id='.$m->id.'&resource_id='.$rsl[0]->id.'"
    onMouseOver="inCB=true;"
    onMouseOut="inCB=false;">'.
    $rsl[0]->GetScanResult(@$rsl[1]).'</a>';
else
    $str_result = "Scheduled";

// *** TR Ext
$tr_ext =
    " id=idMonRow_".$m->id.
    " onmouseover=\"dbrd_row_action(".$m->id.", 'over', '$cl');\" ".
    " onmouseout=\"dbrd_row_action(".$m->id.", 'out', '$cl'); \" ".
    " onmouseup=\"dbrd_row_action(".$m->id.", 'click', '$cl');\"";

echo process_template($template_cp_row, array(
    'MID'			=> $m->id,
    'TR_EXT'        => $tr_ext,
    'CHECKBOX'      => $str_checkbox,
    'ICON'          => $str_icon,
    'MONITOR_NAME'  => $title,
    'GROUP_NAME'    => @$m->data["category_name"],
    'STATUS'        => '<img src="../images/status_'.$status_icon.'.gif" width="9" height="9" alt="" border="0"> '.$status_text,
    'MONITORED'     => $str_monitored,
    'RESULT'        => $str_result,
));
?>